package com.hontech.bread.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.hontech.bread.R
import com.hontech.bread.utils.EmptyEdge

class Rsc3Fragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_rsc3, null)
        initUi(view)
        return view
    }

    private fun initUi(view: View) {

        val tablayout = view.findViewById<TabLayout>(R.id.id_rsc3_tablayout)
        val vp2 = view.findViewById<ViewPager2>(R.id.id_rsc3_vp2)

        Rsc3Adapter(this, tablayout, vp2)
    }
}

private class TabInfo(
    val text: String,
    val fragment: Fragment
)


private class Rsc3Adapter(fragment: Fragment, tabLayout: TabLayout, vp2: ViewPager2) :
    FragmentStateAdapter(fragment) {

    private val fragments = arrayOf(
        TabInfo("Stock", ReplenishFragment2()),
        //  TabInfo("Sales", SalesFragment()),
        // TabInfo("Create Product", CreateProductFragment())
    )

    init {
        vp2.adapter = this
        vp2.offscreenPageLimit = fragments.size
        TabLayoutMediator(tabLayout, vp2, true, false) { tab, index ->
            tab.text = fragments[index].text
        }.attach()
        vp2.isUserInputEnabled = false
        (vp2.getChildAt(0) as RecyclerView).edgeEffectFactory = EmptyEdge()
    }

    override fun getItemCount(): Int {
        return fragments.size
    }

    override fun createFragment(position: Int): Fragment {
        return fragments[position].fragment
    }
}
